<% add_decidim_page_title(t("participatory_process_steps.index.steps_title", scope: "decidim.admin")) %>
<div class="card" id="steps">
  <div class="item_show__header">
    <h1 class="item_show__header-title">
      <%= t("participatory_process_steps.index.steps_title", scope: "decidim.admin") %>
      <% if allowed_to? :create, :process_step %>
        <%= link_to t("actions.new_process_step", scope: "decidim.admin"), new_participatory_process_step_path(current_participatory_process), class: "button button__sm button__secondary ml-auto" %>
      <% end %>
    </h1>
  </div>
  <% if current_participatory_process.steps.any? %>
    <div class="table-stacked">
      <table class="table-list">
        <thead>
          <tr>
            <th><%= t("models.participatory_process_step.fields.title", scope: "decidim.admin") %></th>
            <th><%= t("models.participatory_process_step.fields.start_date", scope: "decidim.admin") %></th>
            <th><%= t("models.participatory_process_step.fields.end_date", scope: "decidim.admin") %></th>
            <th><%= t("models.participatory_process_step.fields.actions", scope: "decidim.admin") %></th>
          </tr>
        </thead>
        <tbody class="sortable" data-sort-url="<%= ordering_participatory_process_steps_path(current_participatory_process) %>">
          <% current_participatory_process.steps.each do |step| %>
            <tr data-id="<%= step.id %>">
              <td data-label="<%= t("models.participatory_process_step.fields.title", scope: "decidim.admin") %>">
                <div class="flex items-center gap-x-2">
                  <%= icon "drag-move-2-line" %>
                  <% if step.active? %>
                    <span class="icon-active"></span>
                  <% end %>
                  <%= link_to translated_attribute(step.title), edit_participatory_process_step_path(current_participatory_process, step) %><br>
                </div>
              </td>
              <td data-label="<%= t("models.participatory_process_step.fields.start_date", scope: "decidim.admin") %>">
                <% if step.start_date %>
                  <%= l step.start_date, format: :decidim_short %>
                <% end %>
              </td>
              <td data-label="<%= t("models.participatory_process_step.fields.end_date", scope: "decidim.admin") %>">
                <% if step.end_date %>
                  <%= l step.end_date, format: :decidim_short %>
                <% end %>
              </td>
              <td class="table-list__actions" data-label="<%= t("models.participatory_process_step.fields.actions", scope: "decidim.admin") %>">
                <button type="button" data-controller="dropdown" data-target="actions-process-step-<%= step.id %>" aria-label="<%= t("decidim.admin.actions.actions_label", resource: translated_attribute(step.title)) %>">
                  <%= icon "more-fill", class: "text-secondary" %>
                </button>

                <div class="inline-block relative">
                  <ul id="actions-process-step-<%= step.id %>" class="dropdown dropdown__action" aria-hidden="true">
                    <% if allowed_to? :update, :process_step, process_step: step %>
                      <li class="dropdown__item">
                        <%= link_to edit_participatory_process_step_path(current_participatory_process, step), class: "dropdown__button" do %>
                          <%= icon "pencil-line" %> <%= t("actions.edit", scope: "decidim.admin") %>
                        <% end %>
                      </li>

                      <hr>
                    <% end %>

                    <% if allowed_to?(:activate, :process_step, process_step: step) && !step.active? %>
                      <li class="dropdown__item">
                        <%= link_to participatory_process_step_activate_path(current_participatory_process, step), method: :post, class: "dropdown__button" do %>
                          <%= icon "check-line" %> <%= t("actions.activate", scope: "decidim.admin") %>
                        <% end %>
                      </li>

                      <hr>
                    <% end %>

                    <% if allowed_to? :destroy, :process_step, process_step: step %>
                      <li class="dropdown__item">
                        <%= link_to participatory_process_step_path(current_participatory_process, step), method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin") }, class: "dropdown__button dropdown__button--danger" do %>
                          <%= icon "delete-bin-line" %> <%= t("actions.destroy", scope: "decidim.admin") %>
                        <% end %>
                      </li>
                    <% end %>
                  </ul>
                </div>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    </div>
  <% end %>
</div>
